package net.cloudcodex.client.campaign.presenter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.cloudcodex.client.CloudCodexEventBus;
import net.cloudcodex.client.campaign.view.CampaignsView;
import net.cloudcodex.client.campaign.view.CampaignsView.Message;
import net.cloudcodex.client.campaign.view.CampaignsViewImpl;
import net.cloudcodex.client.campaign.view.panel.CampaignPanel;
import net.cloudcodex.shared.dto.campaign.character.CharacterHeaderDTO;
import net.cloudcodex.shared.dto.home.CampaignSummaryDTO;
import net.cloudcodex.shared.rpc.CampaignRPCAsync;

import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.inject.Inject;
import com.mvp4g.client.annotation.Presenter;
import com.mvp4g.client.presenter.BasePresenter;

/**
 * 
 * 
 * @author ADaroussin
 */
@Presenter(view = CampaignsViewImpl.class)
public class CampaignsPresenter extends
		BasePresenter<CampaignsView, CloudCodexEventBus> {
	private final Message message;
	private final CampaignRPCAsync campaignService;
	private List<CampaignSummaryDTO> campaignSummaryDTOs = new ArrayList<CampaignSummaryDTO>();
	private CampaignSummaryDTO selectedCampaignSummary;
	private final List<CampaignPresenter> campaignPresenterList = new ArrayList<CampaignPresenter>();
	// private final SortedMap<Integer, CampaignPresenter> campaignPresenterMap
	// = new TreeMap<Integer, CampaignPresenter>();
	private String nickName;
	private Map<Long, Integer> campaignviewIndexed = new HashMap<Long, Integer>();
	private boolean isTabDisplayed = false;

	// private long userId;

	@Inject
	public CampaignsPresenter(Message pMessage, CampaignRPCAsync campaignService) {
		this.message = pMessage;
		this.campaignService = campaignService;
	}

	@Override
	public void bind() {

		this.view.getCampaignTab().addSelectionHandler(
				new SelectionHandler<Integer>() {

					@Override
					public void onSelection(SelectionEvent<Integer> event) {
						if (!campaignPresenterList.isEmpty()) {
							CampaignPresenter selectedPresenter = campaignPresenterList
									.get(event.getSelectedItem().intValue());
							if (selectedPresenter != null
									&& !selectedPresenter.isViewInitialized()) {
								selectedPresenter.initCampaignView();
							}
						}
					}
				});

		// this.view.getCampaignTab().addBeforeSelectionHandler(new
		// BeforeSelectionHandler<Integer>() {
		//
		// @Override
		// public void onBeforeSelection(BeforeSelectionEvent<Integer> event) {
		// System.out.println(" index selected = " + event.getItem());
		// CampaignPresenter selectedPresenter =
		// campaignPresenterList.get(event.getItem());
		// if (selectedPresenter!= null &&
		// !selectedPresenter.isViewInitialized()){
		// System.out.println("init view");
		// selectedPresenter.initCampaignView();
		// }
		// }
		// });
	}

	/**
	 * @param campaignSummaryDTO
	 */
	// public void initCampaignView(CampaignSummaryDTO campaignSummaryDTO) {
	// CampaignPresenter campaignPresenter = eventBus
	// .addHandler(CampaignPresenter.class);
	// int tabIndex = refreshCampaign(campaignSummaryDTO, campaignPresenter);
	// this.campaignviewIndexed.put(campaignSummaryDTO.getId(), tabIndex);
	// campaignPresenterList.add(campaignPresenter);
	// campaignPresenterMap.put(tabIndex, campaignPresenter);
	// }

	/**
	 * @param campaignSummaryDTO
	 * @param campaignPresenter
	 */
	public void refreshCampaign(CampaignSummaryDTO campaignSummaryDTO,
			CampaignPresenter campaignPresenter) {
		campaignPresenter.addCampaignSummary(campaignSummaryDTO);
		// campaignPresenter.initCampaignView();

		campaignPresenter.display();
		campaignPresenter.setSelectedContent(CampaignPanel.CHARACTERS_LIST);

		// return this.view.addCampaignTab(campaignPresenter.getView(),
		// campaignSummaryDTO.getName());
	}

	// private void refreshCampaignView(CampaignSummaryDTO campaignSummaryDTO) {
	// CampaignPresenter campaignPresenter = campaignPresenterMap
	// .get(campaignviewIndexed.get(campaignSummaryDTO.getId()));
	// int index = refreshCampaign(campaignSummaryDTO, campaignPresenter);
	// campaignviewIndexed.remove(campaignSummaryDTO.getId());
	// campaignviewIndexed.put(campaignSummaryDTO.getId(), index);
	// this.view.refreshCampaignTabName(campaignPresenter.getView(),
	// campaignSummaryDTO.getName());
	// }

	public void onInitCampaignsTab(List<CampaignSummaryDTO> campaignSummaryDTOs) {
		this.campaignSummaryDTOs = campaignSummaryDTOs;
		for (CampaignSummaryDTO campaignSummaryDTO : campaignSummaryDTOs) {
			initCampaignPresenter(campaignSummaryDTO);
			// initCampaignView(campaignSummaryDTO);
			// eventBus.addCampaignTab();
		}
		// this.view.selectCampaignTab(null);
		displayTab();
	}

	/**
	 * @param campaignSummaryDTO
	 */
	public void initCampaignPresenter(CampaignSummaryDTO campaignSummaryDTO) {
		CampaignPresenter campaignPresenter = eventBus
				.addHandler(CampaignPresenter.class);
		campaignPresenter.addCampaignSummary(campaignSummaryDTO);
		// campaignPresenter.initCampaignView();
		campaignPresenterList.add(campaignPresenter);
		campaignPresenter.display();
	}

	public void onAddCampaignTab(IsWidget view, String tabName, Long campaignId) {
		int index = this.view.addCampaignTab(view, tabName);
		this.campaignviewIndexed.put(campaignId, index);
	}

	/**
	 * 
	 */
	public void displayTab() {
		isTabDisplayed = true;
		eventBus.addTab(view, this.message.getCampaignTabName());
	}

	// protected boolean isOwner(String ownerNickname) {
	// return nickName.equals(ownerNickname);
	// }
	// protected boolean isOwner(Long ownerId) {
	// if (ownerId != null)
	// return userId==ownerId;
	// return false;
	// }
	protected boolean isGameMaster() {
		return nickName.equals(selectedCampaignSummary.getGameMasterNickname());
	}

	public void onSetNickName(String nickName) {
		this.nickName = nickName;
	}

	// public void onSetuserId(long userId) {
	// this.userId = userId;
	// }
	public void onInitCharacterDescription(Long campaignID, Long characterID) {
		Integer index = campaignviewIndexed.get(campaignID);
		CampaignPresenter selectedPresenter = campaignPresenterList.get(index);
		selectedPresenter.initCharacterDescriptionPopup(characterID);
	}

	public void onSelectCampaignTab(Long campaignId, String selectedContent) {

		Integer index = campaignviewIndexed.get(campaignId);
		this.view.selectCampaignTab(index);
		CampaignPresenter selectedPresenter = campaignPresenterList.get(index);
		selectedPresenter.setSelectedContent(selectedContent);
		selectedPresenter.refreshContent();

		eventBus.selectTab(view);
	}

	public void onAddNewCampaign(CampaignSummaryDTO campaignSummaryDTO) {
		campaignSummaryDTOs.add(campaignSummaryDTO);
		initCampaignPresenter(campaignSummaryDTO);
		if (!isTabDisplayed) {
			displayTab();
		}
	}

	public void onUpdateCampaign(CampaignSummaryDTO campaignSummaryDTO) {

		CampaignPresenter campaignPresenter = campaignPresenterList
				.get(campaignviewIndexed.get(campaignSummaryDTO.getId()));
		campaignPresenter.addCampaignSummary(campaignSummaryDTO);
		campaignPresenter.refreshCampaignInfos();
		this.view.refreshCampaignTabName(campaignPresenter.getView(),
				campaignSummaryDTO.getName());
	}

	public void onAddNewCharacterInCampaign(long campaignId,
			CharacterHeaderDTO characterHeaderDTO) {
		Integer index = campaignviewIndexed.get(campaignId);
		CampaignPresenter campaignPresenter = campaignPresenterList.get(index);
		campaignPresenter.addNewNPCInCampaign(characterHeaderDTO);
	}
}
